package com.tac.controller;

import com.tac.common.result.Result;
import com.tac.pojo.User;
import com.tac.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 注册
     * @param user
     * @return
     */
    @ResponseStatus(HttpStatus.CREATED)
    @PostMapping
    @ApiOperation("注册")
    public Result register(@Valid @RequestBody  User user, Integer verification, HttpServletRequest request){
        log.info("用户：{}，通过地址{}--请求注册",request.getRemoteAddr(),request.getRequestURI());
     return    userService.register(user,verification);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @GetMapping("/{phone}")
    @ApiOperation("获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号",required = true)
    })
    public Result<Integer> verification(@PathVariable Long phone,HttpServletRequest request){
        log.info("用户：{}，通过地址{}--请求获取验证码",request.getRemoteAddr(),request.getRequestURI());
        return userService.verification(phone);
    }

    /**
     * 登录接口
     * @param
     * @param
     * @return
     */
   @PostMapping("/login")
    @ApiOperation("登录接口")
    public Result<User> login(@RequestBody User user,HttpServletRequest request){

        log.info("用户：{}，通过地址{}--请求登录接口",request.getRemoteAddr(),request.getRequestURI());
        return userService.login(user.getPhone(),user.getPassword());
    }
    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation("修改用户信息")
    public Result updateUser(@RequestBody User user,HttpServletRequest request){
        log.info("用户：{}，通过地址{}--请求修改用户信息",request.getRemoteAddr(),request.getRequestURI());
        return userService.updateUser(user);
    }
}
